package com.wkhyapp.lm.weigit;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wkhyapp.lm.R;

import java.util.List;

/**
 * Created by fang on 2021/10/22.
 */

public class FlowLayout extends LinearLayout {

    private LinearLayout layout_v;
    private Context context;
    private Button deleteAll;

    public FlowLayout(Context context) {
        super(context);
        initView(context);
    }

    public FlowLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private void initView(Context context) {
        this.context = context;
        View view = LayoutInflater.from(context).inflate(R.layout.layout_liu, null, false);
        addView(view);
        layout_v = (LinearLayout) view.findViewById(R.id.layout_liu);
        deleteAll = (Button)view.findViewById(R.id.deleteAll);

        //清空历史记录按钮
        deleteAll.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //清空布局内所有view
                layout_v.removeAllViews();
            }
        });
    }


    //等待调用，给集合即可
    public void setList(List<String> list) {

        int len = 0;
        for (int i = 0; i < list.size(); i++) {
            //根据集合长度创建textview
            View view = View.inflate(context, R.layout.text_liu, null);
            TextView text = (TextView)view.findViewById(R.id.text_liu);
            //修改当前TextView
            text.setText(list.get(i) + "");
            Log.i("xxx", "setList: " + list.toString());
            //添加给视图
            layout_v.addView(view);
            //参数控制每一个TextView
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
            layoutParams.rightMargin = 10;
            layoutParams.leftMargin = 10;
            layoutParams.topMargin = 10;
            layoutParams.weight = 1;
            view.setLayoutParams(layoutParams);
        }

    }

}